/*
 * Copyright (c) 2008 Benoit Bouchez, Nicolas Giard
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package com.noocodecommit.wicket.stuff.jquery.plugin.ui.spinner;

import com.noocodecommit.wicket.stuff.jquery.plugin.JQPlugin;

/**
 * 
 * @author Benoit Bouchez
 */
public class JQSpinnerUtils
{

	/**
	 * Create a JQSpinnerBuilder instance on which you can fluently change settings
	 * 
	 * @param id
	 *            wicket component id
	 * @param key
	 *            plugin unique id on which component is attached
	 * @return JQSpinnerBuilder
	 */
	public static JQSpinnerBuilder create(String id, String key)
	{
		return new JQSpinnerBuilder(new JQSpinner(id, (key == null ? JQSpinnerPlugin.DEFAULT_KEY : key)));
	}

	/**
	 * Create a JQSpinnerBuilder instance on which you can fluently change settings
	 * 
	 * @param id
	 *            wicket component id
	 * @return JQSpinnerBuilder
	 */
	public static JQSpinnerBuilder create(String id)
	{
		return new JQSpinnerBuilder(new JQSpinner(id, null));
	}

	/**
	 * @author Benoit Bouchez
	 */
	public static final class JQSpinnerBuilder
	{
		/** JQuery component */
		private JQSpinner component;
		/** JQuery default options */
		private JQSpinnerOptions options;

		/**
		 * Constructor
		 * 
		 * @param component
		 *            widget
		 */
		public JQSpinnerBuilder(JQSpinner component)
		{
			this.component = component;
			this.options = new JQSpinnerOptions();
		}

		/**
		 * Sets plugin init mode
		 * 
		 * @param initMode
		 *            plugin init mode
		 * @return JQSpinnerBuilder
		 */
		public JQSpinnerBuilder initMode(int initMode)
		{
			component.getPlugin().setInitMode(initMode);
			return this;
		}

		/**
		 * Sets plugin selector when initMode is PLUGIN_INIT_MODE_ONCE
		 * 
		 * @param initOnceSelector
		 *            JQuery selector
		 * @return JQSpinnerBuilder
		 */
		public JQSpinnerBuilder initOnceSelector(String initOnceSelector)
		{
			component.getPlugin().setInitOnceSelector(initOnceSelector);
			return this;
		}

		/**
		 * Sets the currency option value
		 * 
		 * @param currency
		 *            spinner currency
		 * @return JQSpinnerBuilder
		 */
		public JQSpinnerBuilder currency(String currency)
		{
			options.setCurrency(currency);
			return this;
		}

		/**
		 * Sets the format option value
		 * 
		 * @param format
		 *            spinner format
		 * @return JQSpinnerBuilder
		 */
		public JQSpinnerBuilder format(String format)
		{
			options.setFormat(format);
			return this;
		}

		/**
		 * Sets the items option value
		 * 
		 * @param items
		 *            spinner items
		 * @return JQSpinnerBuilder
		 */
		public JQSpinnerBuilder items(String items)
		{
			options.setItems(items);
			return this;
		}

		/**
		 * Sets the group option value
		 * 
		 * @param group
		 *            spinner group
		 * @return JQSpinnerBuilder
		 */
		public JQSpinnerBuilder group(String group)
		{
			options.setGroup(group);
			return this;
		}

		/**
		 * Sets the point option value
		 * 
		 * @param point
		 *            spinner point
		 * @return JQSpinnerBuilder
		 */
		public JQSpinnerBuilder point(String point)
		{
			options.setPoint(point);
			return this;
		}

		/**
		 * Sets the incremental option value
		 * 
		 * @param incremental
		 *            spinner incremental
		 * @return JQSpinnerBuilder
		 */
		public JQSpinnerBuilder incremental(boolean incremental)
		{
			options.setIncremental(incremental);
			return this;
		}

		/**
		 * Sets the max option value
		 * 
		 * @param max
		 *            spinner max
		 * @return JQSpinnerBuilder
		 */
		public JQSpinnerBuilder max(int max)
		{
			options.setMax(max);
			return this;
		}

		/**
		 * Sets the min option value
		 * 
		 * @param min
		 *            spinner min
		 * @return JQSpinnerBuilder
		 */
		public JQSpinnerBuilder min(int min)
		{
			options.setMin(min);
			return this;
		}

		/**
		 * Sets the start option value
		 * 
		 * @param start
		 *            spinner start
		 * @return JQSpinnerBuilder
		 */
		public JQSpinnerBuilder start(int start)
		{
			options.setStart(start);
			return this;
		}

		/**
		 * Sets the stepping option value
		 * 
		 * @param stepping
		 *            spinner stepping
		 * @return JQSpinnerBuilder
		 */
		public JQSpinnerBuilder stepping(int stepping)
		{
			options.setStepping(stepping);
			return this;
		}

		/**
		 * Sets the decimals option value
		 * 
		 * @param decimals
		 *            spinner decimals
		 * @return JQSpinnerBuilder
		 */
		public JQSpinnerBuilder decimals(int decimals)
		{
			options.setDecimals(decimals);
			return this;
		}

		/**
		 * Sets the init function
		 * 
		 * @param initScript
		 *            spinner init function
		 * @return JQSpinnerBuilder
		 */
		public JQSpinnerBuilder initScript(String initScript)
		{
			options.setInitScript(initScript);
			return this;
		}

		/**
		 * Sets the change function
		 * 
		 * @param changeScript
		 *            spinner change function
		 * @return JQSpinnerBuilder
		 */
		public JQSpinnerBuilder changeScript(String changeScript)
		{
			options.setChangeScript(changeScript);
			return this;
		}

		/**
		 * Sets the up function
		 * 
		 * @param upScript
		 *            spinner up function
		 * @return JQSpinnerBuilder
		 */
		public JQSpinnerBuilder upScript(String upScript)
		{
			options.setUpScript(upScript);
			return this;
		}

		/**
		 * Sets the down function
		 * 
		 * @param downScript
		 *            spinner down function
		 * @return JQSpinnerBuilder
		 */
		public JQSpinnerBuilder downScript(String downScript)
		{
			options.setDownScript(downScript);
			return this;
		}

		/**
		 * Sets the spin function
		 * 
		 * @param spinScript
		 *            spinner spin function
		 * @return JQSpinnerBuilder
		 */
		public JQSpinnerBuilder spinScript(String spinScript)
		{
			options.setSpinScript(spinScript);
			return this;
		}

		/**
		 * @return JQSpinnerBuilder
		 */
		public JQSpinner build()
		{
			if (component.getPlugin().getInitMode() == JQPlugin.PLUGIN_INIT_ONCE)
				component.getPlugin().setOptions(options);
			else
				component.setOptions(options);
			return component;
		}
	}
}